import ajaxAsync from 'vue-xiaobu-utils'
import { CanRechargeModel } from '../model/CanRechargeModel'
import { CouponsModel } from '../model/CouponsModel'

/**
 * 获取手机号的绑卡状态
 * @param {number} phoneNo 手机号码
 */
function getUserCanRecharge(phoneNo) {
  let postData = {
    PHONE_NO: phoneNo
  }
  return ajaxAsync('/buscard/virtual/canRecharge', postData).then(res => {
    return new CanRechargeModel(res.STATE, res.NAME, res.CARD_NO)
  })
}

/**
 * 获取商品的价格
 * @param {string} spuId 商品id
 */
function getBusCardPrice(spuId) {
  let postData = {
    SPU_ID: spuId
  }
  return ajaxAsync('/goods/detail', postData).then(res => {
    return res.SKU_MAP
  })
}

/**
 * 获取支付串
 * @param {string} skuId sku编号
 * @param {string} payment 支付方式
 * @param {string} phoneNo 手机号
 * @param {string} couponNo 优惠券号
 * @param {float} price 支付金额
 * @param {string} cardNo 卡号
 * @param {string} cardInstanceId 卡实例编号
 * @param {int} cardType 卡类型编号
 * @param {string} url 回调前端地址
 */
function getPaymentStr(
  skuId,
  payment,
  phoneNo,
  couponNo,
  price,
  cardNo,
  cardInstanceId,
  cardType,
  url
) {
  // 由于有普通云卡和老年云卡，所以发送充值协议时，前端不能指定卡类型CARD_TYPE
  let postData = {
    SKU_ID: skuId,
    PAYMENT: payment,
    COUPON_NO: couponNo,
    PRICE: price,
    CARD_INSTANCE_ID: cardInstanceId,
    // CARD_TYPE: cardType,
    FRONT_NOTIFY_URL: url
  }
  if (phoneNo || cardNo) {
    postData.PHONE_NO = phoneNo
    postData.CARD_NO = cardNo
  }
  return ajaxAsync('/fundaccount/recharge', postData).then(res => {
    let paymentObj = { orderId: res.ORDER_ID, paymentStr: res.PAYMENT_STR }
    return paymentObj
  })
}

/**
 * 获取云卡优惠券列表
 * @param {string} storeId 店铺编号
 * @param {string} skuId sku编号
 * @param {int} skuNum sku数量
 */
function getCouponsList(storeId, skuId, skuNum) {
  let postData = {
    STORE_ID: storeId,
    SKU_ID: skuId,
    SKU_NUM: skuNum
  }
  return ajaxAsync('/order/user/sku_coupons', postData).then(res => {
    let couponsItem = []
    res.LIST.forEach(item => {
      couponsItem.push(
        new CouponsModel(item.NAME, item.PAY_PRICE, item.COUPON_ID, item.STATE)
      )
    })
    let couponsObj = {
      count: res.COUNT,
      list: couponsItem
    }
    return couponsObj
  })
}

export { getUserCanRecharge, getBusCardPrice, getPaymentStr, getCouponsList }
